package com.dailyblue.java.project.bookstore.count.service.impl;

import com.dailyblue.java.project.bookstore.count.service.BookCountService;
import com.dailyblue.java.project.bookstore.count.util.JsonResult;
import com.dailyblue.java.project.bookstore.count.util.ResultTool;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class BookCountServiceImpl implements BookCountService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public JsonResult addBookCount(int bookId) {
        HashOperations<String, String, String> hash = stringRedisTemplate.opsForHash();
        String v = hash.get("BOOK_COUNT", bookId + "");
        if (v == null) {
            v = "0";
        }
        int m = Integer.parseInt(v);
        hash.put("BOOK_COUNT", bookId + "", m + 1 + "");
        return ResultTool.success(m + 1);
    }
}
